package cn.service;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;

public class SocketClient {
    public static void main(String[] args) {
        Socket socket = new Socket();
        InputStream in = null;
        OutputStream out = null;
        try {
            socket.connect(new InetSocketAddress("127.0.0.1",12345));
            out = socket.getOutputStream();
            out.write("hello world".getBytes());
            byte[] data = new byte[1024];
            int len = 0;
            in = socket.getInputStream();
            while ((len = in.read(data)) != -1 ) {
                 System.out.println(new String(data,0,len));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
